package com.aeye.mbr.upms.server.controller;

import com.aeye.mbr.common.base.BaseController;
import com.aeye.mbr.common.util.AESUtil;
import com.aeye.mbr.common.util.PropertiesFileUtil;
import com.aeye.mbr.common.util.StringUtils;
import com.aeye.mbr.upms.dao.model.UpmsPermission;
import com.aeye.mbr.upms.dao.model.UpmsSystem;
import com.aeye.mbr.upms.dao.model.UpmsUser;
import com.aeye.mbr.upms.rpc.api.UpmsApiService;
import com.aeye.mbr.upms.rpc.api.UpmsSystemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

/**
 * 后台controller
 * Created by ZhangShuaeye.mbr on 2017/01/19.
 */
@Controller
@RequestMapping("/manage")
@Api(value = "后台管理", description = "后台管理")
public class ManageController extends BaseController {

	private static Logger _log = LoggerFactory.getLogger(ManageController.class);

	@Autowired
	private UpmsSystemService upmsSystemService;

	@Autowired
	private UpmsApiService upmsApiService;


	@ApiOperation(value = "后台首页")
	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public String index(HttpServletRequest request,ModelMap modelMap) {
		// 已注册系统
		UpmsSystem system = new UpmsSystem();
		system.setStatus((byte)1);
		List<UpmsSystem> upmsSystems = upmsSystemService.select(system);
		modelMap.put("upmsSystems", upmsSystems);
		// 当前登录用户权限
		Subject subject = SecurityUtils.getSubject();
		String username = (String) subject.getPrincipal();
		UpmsUser upmsUser = upmsApiService.selectUpmsUserByUsername(username);
		List<UpmsPermission> upmsPermissions = upmsApiService.selectUpmsPermissionByUpmsUserId(upmsUser.getUserId());
		modelMap.put("upmsPermissions", upmsPermissions);
		/*if(wechatMpBusiApiService.hasBindWechat(PropertiesFileUtil.getInstance("qrcode").get("wechat.configId"),upmsUser.getUserId()))
		{
		}else
		{
			modelMap.put("wechatNotBinding","true");
		}
		String qrCodeBindingAddress = PropertiesFileUtil.getInstance("qrcode").get("wechat.banding.address.prefix");
		try {
			modelMap.put("wechatBindingAddress",qrCodeBindingAddress+"/"+ URLEncoder.encode(AESUtil.AESEncode(""+upmsUser.getUserId()),"utf-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}*/
		return "/manage/index.jsp";
	}


	public static String getIp(HttpServletRequest request) {
		String ip = request.getHeader("X-Forwarded-For");
		if(StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)){
			//多次反向代理后会有多个ip值，第一个ip才是真实ip
			int index = ip.indexOf(",");
			if(index != -1){
				return ip.substring(0,index);
			}else{
				return ip;
			}
		}
		ip = request.getHeader("X-Real-IP");
		if(StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)){
			return ip;
		}
		return request.getRemoteAddr();
	}
}